package com.oss.robot.link;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.BulletList;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;

import com.oss.model.Constants;
import com.oss.model.Link;
import com.oss.model.Task;
import com.oss.robot.AbstractRetrieveRobot;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.fetcher.FeedFetcher;
import com.sun.syndication.fetcher.impl.FeedFetcherCache;
import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache;
import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher;

/**
 * 解析一个页面得到所有的RSS源，然后进行RSS源解析
 * 
 * @author xueqiang.mi
 * @since 2010-7-19
 */
public class TheServerSideRobot extends AbstractRetrieveRobot {

	static public final String URL = "http://www.theserverside.com/rss";
	static public final String SOURCE = "TheServerSide.COM";
	static public final Integer TASK_NUMBER_PER_TIME = 2;

	@Override
	@SuppressWarnings("unchecked")
	public void extractData(Task task) throws Exception {
		FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance();
		FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache);
		SyndFeed feed = null;
		try {
			feed = feedFetcher.retrieveFeed(new URL(task.getLink()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<SyndEntry> entries = feed.getEntries();
		for (SyndEntry entry : entries) {
			Link lnk = new Link();
			lnk.setSource(SOURCE);
			lnk.setName(entry.getTitle());
			String desc = entry.getDescription().getValue();
			lnk.setDescription(desc);
			lnk.setUrl(entry.getLink());
			lnkService.add(lnk);
		}
	}

	@Override
	public String getSource() {
		return SOURCE;
	}

	@Override
	public Integer getTasksNumber() {
		return TASK_NUMBER_PER_TIME;
	}

	@Override
	public List<Task> retrieveLinks() throws Exception {
		Parser parser = new Parser(URL);
		NodeList nodeList = parser.extractAllNodesThatMatch(new AndFilter(
				new TagNameFilter("Div"), new HasAttributeFilter("class",
						"col1")));
		Node node = nodeList.elementAt(0).getFirstChild();
		while (!(node instanceof BulletList)) {
			node = node.getNextSibling();
		}

		nodeList = node.getChildren().extractAllNodesThatMatch(
				new AndFilter(new TagNameFilter("a"), new HasAttributeFilter(
						"class", "rssFeed")), true);

		List<Task> tasks = new ArrayList<Task>();
		for (Node linkTag : nodeList.toNodeArray()) {
			LinkTag link = (LinkTag) linkTag;
			Task task = new Task(link.getLink(), SOURCE, Constants.ENTRY_LINK);
			tasks.add(task);
		}
		return tasks;
	}
}
